﻿using System;
using owasp = org.owasp.validator.html;
using SocialFront.Entities;
using SocialFront.Providers.Blog;
using SocialFront.Providers.Member;
using umbApi = umbraco;

namespace SocialFront.Web.usercontrols.Blog
{
    public partial class CreateBlog : System.Web.UI.UserControl
    {
        private Entities.Blog blog;

        protected void Page_Init(object sender, EventArgs e)
        {
            ((umbApi.UmbracoDefault)Page).ValidateRequest = false;
            Utilities.AddTinyMce();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["action"] == "edit")
                {
                    if (Request.QueryString["id"] != "")
                    {
                        int blogId = Convert.ToInt32(Request.QueryString["id"]);
                        blog = BlogProvider.Provider.GetBlogById(blogId);
                    }
                }
            }
        }

        protected void bCreateBlog_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                blog = new Entities.Blog
                                         {

                                             Title = blogTitle.Text,
                                             Content = blogContent.Text,
                                             Enabled = true,
                                             Tags = Utilities.ConvertCsvToTags(blogTags.Text),
                                             Member = MemberProvider.Provider.GetCurrentMember()
                                         };

                blog = BlogProvider.Provider.CreateBlog(blog);
                Response.Redirect(blog.Url);
            }
        }
    }
}